<template>
  <el-dialog title="添加菜单按钮" :visible.sync="dialogFormVisible" top="10vh" width="55%">
    <el-form ref="formInline" :inline="true" :model="formInline" :rules="rules" :label-position="labelPosition"  label-width="100px" size="small">

      <el-row>
        <el-col :span="12">
      <el-form-item label="按钮名称:" prop="name">
        <el-input v-model="formInline.name" :style="{width: '140%'}" placeholder="请输入菜单名称"></el-input>
      </el-form-item>
        </el-col>
      <el-col :span="12">
        <el-form-item label="请求方式:" prop="method">
          <el-select :style="{width: '130%'}" v-model="formInline.method" clearable  placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="权限值:" prop="value">
          <el-input v-model="formInline.value" :style="{width: '140%'}" placeholder="请输入权限值"></el-input>
        </el-form-item>
        </el-col>
        <el-col :span="12">
      <el-form-item label="接口地址:" prop="api">
        <el-input v-model="formInline.api" :style="{width: '140%'}" placeholder="请输入组件地址"></el-input>
      </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm('formInline')">确 定</el-button>
      <el-button @click="resetForm('formInline')">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { addMenuButtonData } from '@/api/menuButton'
  import { successMsg } from '@/utils/tools'
  export default {
    name: 'Insert',
    props:{ /*----接收数据----*/
      menu_id:{
        type:Number,
        required:true
      }
    },
    data() {
      return {
        dialogFormVisible: false,
        labelPosition:'right',
        options: [{
          value: 0,
          label: '查询(get)'
        }, {
          value: 1,
          label: '新增(post)'
        }, {
          value: 2,
          label: '编辑(put)'
        }, {
          value: 3,
          label: '删除(delete)'
        }, {
          value: 0,
          label: '单例(get)'
        }],
        formInline: {
          name: '',
          method:'',
          value:'',
          api:'',
          menu:Number,
        },
        rules:{
          name:[
            { required: true, message: '请输入按钮名称', trigger: 'blur' },
          ],
          method:[
            { required: true, message: '请选择按钮请求方式', trigger: 'change' },
          ],
          value:[
            { required: true, message: '请输入权限值', trigger: 'blur' },
          ],
          api:[
            { required: true, message: '请输入按钮地址', trigger: 'blur' },
          ],
        }
      };
    },
    methods:{
      submitForm(formName) {
        this.formInline.menu=this.menu_id
        this.$refs[formName].validate((valid) => {
          if(valid){
            addMenuButtonData(this.formInline).then(res=>{
              if(res.code===2000) {
                successMsg(res.msg)
                this.$refs[formName].resetFields();
                this.$emit('reloadTable')
                this.dialogFormVisible = false
              }
            })
          }else{
            return false;
          }
        });
      },
      resetForm(formName){
        this.$refs[formName].resetFields();
        this.dialogFormVisible = false
      },
      handleNodeClick(node){
        this.formInline.parent = node.id;
        this.$refs.selectTree.blur();
      }
    }
  }
</script>

<style>
  .main-select-el-tree .el-tree-node .is-current > .el-tree-node__content{font-weight: bold; color: #409eff;}
  .main-select-el-tree .el-tree-node.is-current > .el-tree-node__content{font-weight: bold; color: #409eff;}
  .div_padding{
    padding: 5px 5px;
  }
  .label_padding1{
    padding-left: 50px;
  }
  .label_padding2{
    padding-left: 65px;
  }
  .input_width1{
    width: 150%;
  }
  .input_width2{
    width: 145%;
  }
</style>
